package com.example.util;

import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.Module;
import com.alibaba.jvm.sandbox.api.annotation.Command;
import com.alibaba.jvm.sandbox.api.listener.ext.Advice;
import com.alibaba.jvm.sandbox.api.listener.ext.AdviceListener;
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatchBuilder;
import com.alibaba.jvm.sandbox.api.resource.ModuleEventWatcher;
import org.kohsuke.MetaInfServices;

import javax.annotation.Resource;
import java.util.*;
import java.util.logging.Logger;


@MetaInfServices(Module.class)
@Information(id = "tx01", author = "tx", version = "0.0.1")
public class hook_jvm implements Module {
    private Logger LOG = Logger.getLogger(hook_jvm.class.getName());

    @Resource
    private ModuleEventWatcher moduleEventWatcher;

    @Command("tx01")
    public void tx01(final Map<String, String> param) {
        new EventWatchBuilder(moduleEventWatcher).onClass("com.example.server.service.impl.FmcFarmBatchServiceImpl")
                .onBehavior("updateFarmBatch")
                .onWatch(new AdviceListener() {
                    @Override
                    protected void afterReturning(Advice advice) throws Throwable {
                        LOG.info("method---" + advice.getBehavior().getName());
                        advice.changeParameter(0, Long.parseLong("781559741738729472"));
                    }
                });
    }

    @Command("tx02")
    public void tx02(final Map<String, String> param) {
        new EventWatchBuilder(moduleEventWatcher)
                .onClass("com.example.server.service.impl.MyEmployeeServiceImpl")
                .onBehavior("addEmp")
                .onWatch(new AdviceListener() {
                    @Override
                    protected void before(Advice advice) throws Throwable {
                        LOG.info("method---" + advice.getBehavior().getName());
                        advice.changeParameter(0, "2");
                    }
                });
    }
}